﻿#ifndef SHAREDMEMORY_H
#define SHAREDMEMORY_H

#include <string>
#include <memory>

class SharedMemoryImpl; // hide implementation detail
class SharedMemory
{
public:
    enum AccessMode
    {
        ReadOnly = 0,
        ReadWrite
    };

public:
    explicit SharedMemory(const std::string &key);
    ~SharedMemory();

    bool Create(size_t size);

    bool Attach(SharedMemory::AccessMode mode);
    bool Detach();
    bool IsAttached() const;

    int Write(unsigned char *data, size_t size);
    int Read(unsigned char *buf, size_t size);

    unsigned char *Data();
    int DataLen();

    void Lock();
    void Unlock();

private:
    std::shared_ptr<SharedMemoryImpl> m_impl{nullptr};
};

#endif // SHAREDMEMORY_H
